package com.itcast.contraller;

import com.itcast.pojo.Result;
import com.itcast.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    /*
    * 本地存储
    * */
//    @PostMapping("/upload")
//    public Result upload(String name, String age, MultipartFile file) throws IOException {
//        log.info("文件上传：{},{},{}",name,age,file.getOriginalFilename());
//        String filename = file.getOriginalFilename();
//        String fileUuid = UUID.randomUUID().toString();
//
//        file.transferTo(new File("D:\\studyFile\\image\\"+fileUuid+"-"+filename));
//
//        return Result.success();
//    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件上传：{}",file.getOriginalFilename());
        //文件交给oss存储
        String message =aliyunOSSOperator.upload(file.getBytes(),file.getOriginalFilename());
        log.info("文件上传信息：{}",message);
        return Result.success(message);
    }

}
